
function $(id){
    return document.querySelector(`#${id}`);
}
//获得需要操作的节点(对象)
let userNameInput = $("userName");
let userpwdInput = $("pwd");
let btnObj = $("btn");
//3 给按钮绑定事件
btnObj.onclick=()=>{
    // 1 创建核心对象
    let  xmlHttpRequest = null;
    var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlHttpRequest=new XMLHttpRequest();
        }else
        {// code for IE6, IE5
            xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
        }
        // 2 创建连接
        xmlHttpRequest.open("post","/userlogin",true);
        xmlHttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        //3 发送请求
        xmlHttpRequest.send(`name=${userNameInput.value}&pwd=${userpwdInput.value}`)
        //4处理响应
        xmlHttpRequest.onreadystatechange=()=>{
            if(xmlHttpRequest.readyState==4 && xmlHttpRequest.status==200){
                  let data =  xmlHttpRequest.responseText; 
                  //console.log(data);
                  let resultObj =JSON.parse(data);
                 //判断是否登录成功
                 if(resultObj.code===0){
                     alert(resultObj.msg);
                     window.location.href='/pages/studentList.html';
                 }else{
                    alert(resultObj.msg);

                 }
            
            }
        }

}
